import Mock, { Random } from 'mockjs'
Mock.setup({
    timeout:'200-600'
})
const List=Mock.mock({
    'user|30': [{   // 随机生成30个数组元素
            username: '@word(3, 5)',
            password: '123456',
            "roles|1": ['editor','manager'],
            name: '@cname',
            "age|20-23": 23,
            "job|1":['前端工程师','后端工程师','软件测试'],
            token:'@guid',
            id: '@id()',
    }]
})
const userList = {  //定义用户数据
    data: {
        total: 31,
        pagesize:4,
        //前两个用户数据分别固定设为管理员和普通用户，为后续权限设置做准备，其他用户随机生成
        userinfo: [{  
            username: 'admin',
            password: '123456',
            roles: 'admin',
            name: '张三',
            age: 23,
            job: '前端工程师',
            token: '000111222333444555666',
            id: '100',
        }

        ],
        
        meta: {
            status: 200,
            message: 'success',
        }
    },
};
Mock.mock("/user",'get',()=>{
    return{
        meta: {
            msg: 'success',
            status: 200
        },
        List,
        userList

    }
})

Mock.mock("/login", 'post', req => { //路径与请求方式
  const { params } =JSON.parse(req.body); //将传递进来的数据保存
  const{username,userpass}=params;
  for (let i = 0; i < userList.data.userinfo.length; i++) { 
      //判断userList中是否存在该用户并且用户密码是否正确
      if (username === userList.data.userinfo[i].username && userpass === userList.data.userinfo[i].password) {
          return {
              meta: {
                  msg: 'success',
                  status: 200
              },
              user: {
                  username: userList.data.userinfo[i].username,
                  roles: userList.data.userinfo[i].roles,
                  name:userList.data.userinfo[i].name,
              }
          }
      }
  }
  return {
      meta: {
          msg: 'error',
          status: 201
      }
  }
})
//添加方法
Mock.mock('/adduser','post',req=>{
    const {params}=JSON.parse(req.body);
    console.log(params);
    for(let i=0;i<List.user.length;i++){
        if(List.user[i].id==params.id){
               return {
                meta: {
                    msg: 'error',
                    status: 201
                }
               }
        }
    }
    List.user.unshift(params)
    return{
        List
    }
})
//修改方法
Mock.mock("/update",'post',req=>{
    const {params}=JSON.parse(req.body);
    for(let i=0;i<List.user.length;i++){
        if(List.user[i].id==params.id){
                List.user.splice(i,1,params)
        }
    }
    return{
        List
    }
})
//搜索方法
Mock.mock("/search",'post',req=>{
    const {params}=JSON.parse(req.body); //将传递进来的数据保存
    const {userinfo}=params;
    const newarr=[];
    for(let i=0;i<List.user.length;i++){
        if(List.user[i].username.indexOf(userinfo)>=0){
            newarr.push(List.user[i])
        }
    }
    return{
        newarr
    }
})
//删除方法
Mock.mock("/delete",'post',req=>{
    const {params}=JSON.parse(req.body); //将传递进来的数据保存
    const {userid}=params;
    for(let i=0;i<List.user.length;i++){
        if(userid==List.user[i].id){
            List.user.splice(i,1)
            
        }
    }
    return{
        meta: {
            msg: 'success',
            status: 200
        },
        List
    }
})
//定义请求方法与路径
export default {
    'post|/login': userList,
}